/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.controller;

import com.techshop.service.RevenueReportService;
import com.techshop.viewmodel.Inventory;
import com.techshop.viewmodel.InventoryByStore;
import com.techshop.viewmodel.InventoryPhone;
import com.techshop.viewmodel.RevenueExpenditure;
import com.techshop.viewmodel.RevenueExpenditureByStore;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

/**
 *
 * @author Hai-PC
 */
@ManagedBean(name = "revenueReportController")
@ViewScoped
public class RevenueReportController extends AbstractController
{
    @ManagedProperty(value = "#{revenueReportService}")
    RevenueReportService revenueReportService;
    
    private List<RevenueExpenditure> revenueExpenditureList = new ArrayList();
    private List<RevenueExpenditureByStore> revenueExpenditureByStoreList = new ArrayList();
    private List<InventoryByStore> inventoryByStoreList = new ArrayList();
    private List<InventoryPhone> inventoryPhoneList = new ArrayList();
    
    private int selectedMonth;
    private int selectedYear;
    
    private CartesianChartModel revenueExpenditureModel;
    private CartesianChartModel inventoryModel;

    public void updateRevenueExpenditureList()
    {
        List<Object[]> resultSet = revenueReportService.getRevenueAndExpenditure(selectedMonth, selectedYear);
        List<RevenueExpenditure> list = new ArrayList<>();
        for (Object[] row : resultSet)
        {
            Date date = (Date)row[0];
            int revenue = ((BigDecimal)row[1]).intValue();
            int expenditure = ((BigDecimal)row[2]).intValue();
            int profit = ((BigDecimal)row[3]).intValue();
            list.add(new RevenueExpenditure(date, revenue, expenditure, profit));
        }
        setRevenueExpenditureList(list);
    }
    
    public void updateRevenueExpenditureByStoreList()
    {
        List<Object[]> resultSet = revenueReportService.getRevenueAndExpenditureByStore(selectedMonth, selectedYear);
        List<RevenueExpenditureByStore> list = new ArrayList<>();
        for (Object[] row : resultSet)
        {
            String store = (String)row[0];
            Date date = (Date)row[1];
            int revenue = ((BigDecimal)row[2]).intValue();
            int expenditure = ((BigDecimal)row[3]).intValue();
            int profit = ((BigDecimal)row[4]).intValue();
            list.add(new RevenueExpenditureByStore(store, date, revenue, expenditure, profit));
        }
        setRevenueExpenditureByStoreList(list);
    }
    
    public void updateInventoryByStoreList()
    {
        List<Object[]> resultSet = revenueReportService.getInventoryByStore(selectedMonth, selectedYear);
        List<InventoryByStore> list = new ArrayList<>();
        for (Object[] row : resultSet)
        {
            String storeName = (String)row[0];
            String productName = (String)row[1];
            String colorName = (String)row[2];
            int importedQuantity = ((BigDecimal)row[3]).intValue();
            int exportedQuantity = ((BigDecimal)row[4]).intValue();
            list.add(new InventoryByStore(storeName, productName, colorName, importedQuantity, exportedQuantity));
        }
        setInventoryByStoreList(list);
    }
    
    public void createDeliveryNoteRevenueModel()
    {
        updateRevenueExpenditureList();
        revenueExpenditureModel = new CartesianChartModel();
        ChartSeries series = new ChartSeries();
        series.setLabel("Doanh thu");
        DateFormat dateFormat = new SimpleDateFormat("dd");
        int days = 31;
        switch (selectedMonth)
        {
            case 4:
            case 6:
            case 9:
            case 11:
            {
                days = 30;
                break;
            }
            case 2:
            {
                days = 29;
                break;
            }
        }
        for (int i = 1; i < days + 1; i++)
        {
            boolean isZero = true;
            for (RevenueExpenditure d : revenueExpenditureList)
            {
                if (Integer.parseInt(dateFormat.format(d.getDate())) == i)
                {
                    series.set(dateFormat.format(d.getDate()), d.getProfit());
                    isZero = false;
                    break;
                }
            }
            if (isZero)
            {
                series.set(String.valueOf(i), 0);
            }
        }
        revenueExpenditureModel.addSeries(series);
    }
    
    public void createInventoryModel()
    {
        List<Object[]> resultSet = revenueReportService.getInventoryPhone(selectedMonth, selectedYear);
        List<InventoryPhone> list = new ArrayList<>();
        for (Object[] row : resultSet)
        {
            String productName = (String)row[0];
            int importedQuantity = ((BigDecimal)row[1]).intValue();
            int exportedQuantity = ((BigDecimal)row[2]).intValue();
            list.add(new InventoryPhone(productName, importedQuantity, exportedQuantity));
        }
        inventoryPhoneList = list;
        inventoryModel = new CartesianChartModel();
        ChartSeries series = new ChartSeries();
        series.setLabel("SL nhập");
        for (InventoryPhone t : list)
        {
            series.set(t.getProductName(), t.getImportedQuantity());
        }
        inventoryModel.addSeries(series);
        ChartSeries seriesA = new ChartSeries();
        seriesA.setLabel("SL xuất");
        for (InventoryPhone t : list)
        {
            seriesA.set(t.getProductName(), t.getExportedQuantity());
        }
        inventoryModel.addSeries(seriesA);
    }
    
    public RevenueReportService getRevenueReportService() {
        return revenueReportService;
    }

    public void setRevenueReportService(RevenueReportService revenueReportService) {
        this.revenueReportService = revenueReportService;
    }
    
    public List<RevenueExpenditure> getRevenueExpenditureList()
    {
        return revenueExpenditureList;
    }

    public void setRevenueExpenditureList(List<RevenueExpenditure> revenueExpenditureList)
    {
        this.revenueExpenditureList = revenueExpenditureList;
    }

    public List<RevenueExpenditureByStore> getRevenueExpenditureByStoreList()
    {
        return revenueExpenditureByStoreList;
    }

    public void setRevenueExpenditureByStoreList(List<RevenueExpenditureByStore> revenueExpenditureByStoreList)
    {
        this.revenueExpenditureByStoreList = revenueExpenditureByStoreList;
    }
    
    public int getSelectedMonth()
    {
        return selectedMonth;
    }

    public void setSelectedMonth(int selectedMonth)
    {
        this.selectedMonth = selectedMonth;
    }

    public int getSelectedYear()
    {
        return selectedYear;
    }

    public void setSelectedYear(int selectedYear)
    {
        this.selectedYear = selectedYear;
    }

    public CartesianChartModel getRevenueExpenditureModel()
    {
        return revenueExpenditureModel;
    }

    public List<InventoryByStore> getInventoryByStoreList()
    {
        return inventoryByStoreList;
    }

    public void setInventoryByStoreList(List<InventoryByStore> inventoryByStoreList)
    {
        this.inventoryByStoreList = inventoryByStoreList;
    }

    public CartesianChartModel getInventoryModel()
    {
        return inventoryModel;
    }

    public List<InventoryPhone> getInventoryPhoneList()
    {
        return inventoryPhoneList;
    }

    public void setInventoryPhoneList(List<InventoryPhone> inventoryPhoneList)
    {
        this.inventoryPhoneList = inventoryPhoneList;
    }
}